package com.example.cet4try.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.cet4try.R;
import com.example.cet4try.Utils.MyAsyncTask;
import com.example.cet4try.myDialog.LoadDialog;
import com.example.cet4try.Utils.MySQLUtils;

import java.util.HashMap;

public class ActivityLogin extends AppCompatActivity {

    private Button btn_login;
    private EditText username,pwd;
    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        init();

        String husername = preferences.getString("username","");
        if (husername.length() > 1){
            startActivity(new Intent(getApplicationContext(),Main2Activity.class));
            finish();
            return;
        }

        TextView tv_regist = findViewById(R.id.tv_regist);
        tv_regist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(),ActivityRegist.class));
            }
        });
        TextView tv_yklogin = findViewById(R.id.tv_yklogin);
        tv_yklogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("nickname","游客");
                editor.putString("username","yk");
                editor.apply();
                startActivity(new Intent(getApplicationContext(),Main2Activity.class));
                finish();
            }
        });
        TextView tv_forget = findViewById(R.id.tv_forget);
        tv_forget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(),ActivityTest.class));
//                finish();
            }
        });

        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                setBG();
            }
        };
        username.addTextChangedListener(textWatcher);

        pwd.addTextChangedListener(textWatcher);

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(ActivityLogin.this,"登陆",Toast.LENGTH_SHORT).show();
                final String susername = username.getText().toString();
                final String spwd = pwd.getText().toString();

                boolean isAdmin = false;

                if (susername.equals("admin")&&spwd.equals("123456")){
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putString("nickname","管理员");
                    editor.putString("username","admin");
                    editor.apply();

                    isAdmin = true;
                }

                login(isAdmin);

            }
        });


    }

    private void init(){
        btn_login = findViewById(R.id.btn_login);
        username = findViewById(R.id.username);
        pwd = findViewById(R.id.pwd);

        preferences = getSharedPreferences("App_Shared_Data", 0);//ActivityLogin.class.toString()

    }

    private void setBG(){
        if (checkText()){
            btn_login.setEnabled(true);
            btn_login.setBackground(getResources().getDrawable(R.drawable.button_login2));
        }
        else{
            btn_login.setEnabled(false);
            btn_login.setBackground(getResources().getDrawable(R.drawable.button_login1));
        }

    }

    private boolean checkText(){
        if (username.getText().toString().length() < 1)
            return false;
        if (pwd.getText().toString().length() < 1)
            return false;

        return true;
    }



    private void login(final boolean isAdmin){
        MyAsyncTask task = new MyAsyncTask(ActivityLogin.this,"正在登陆");
        task.setOnExecute(new MyAsyncTask.OnExecute() {
            private boolean logFlag;
            private String susername,spwd;
            @Override
            public void before() {
                logFlag = false;
                susername = username.getText().toString();
                spwd = pwd.getText().toString();
            }
            @Override
            public void main() {
                if (!isAdmin){
                    HashMap<String, Object> map = MySQLUtils.SelectUser(susername);
                    if(map != null){
                        String p = (String) map.get("password");
                        if (p != null){
                            SharedPreferences.Editor editor = preferences.edit();
                            if (p.equals(spwd)){
                                logFlag = true;
                                String nickname = (String) map.get("nickname");
                                editor.putString("nickname",nickname);
                                editor.putString("username",susername);
                            }
                            else {
                                editor.putString("username","");
                                editor.putString("nickname","");
                            }
                            editor.apply();
                        }

                    }
                }
                else
                    logFlag = true;
            }
            @Override
            public void after() {
                if (logFlag){
                    startActivity(new Intent(getApplicationContext(),Main2Activity.class));
                    finish();
                }
                else
                    Toast.makeText(ActivityLogin.this,"用户名或密码不正确！",Toast.LENGTH_SHORT).show();
            }
        });
        task.execute();
    }

}
